#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	freopen("init.in", "r", stdin);
	string n;
	int m;
	cin >> n >> m;
	int sum = 0;

	for (size_t i = 0; i < n.size(); ++i)
	{
		int temp = 0;

		if (isdigit(n[i]))
			temp = n[i] - 48;
		else if (islower(n[i]))
			temp = n[i] - 86;
		else if (isupper(n[i]))
			temp = n[i] - 55;

		sum = sum * m + temp;
	}

	printf("%d\n", sum);
}

